const app = getApp()
const backgroundAudioManager = wx.getBackgroundAudioManager()
var util = require('../../utils/util.js');
var requestApi = require('../../request/requestApi')
// var Promise = require('../../plugins/es6-promise.js')
var isChangeSmall=0;
var stopTimerControl=false;
Page({
    data: {
        isIphoneX: false,
        screenWidth: '',
        screenHeight: '',
        networkFormatData:[],
        delBtnWidth: 60,
        pageno:1,  //分页
        indexTimer:null,
        playingIndex:0, //正在播放章节
        playingCurrentPro:0, //正在播放作品
        isPageNext:true,  //判断是否进行下一页
        playBarControlWidth:0,  //播放进度位置
        playBarControlWidthMax:540, //播放条长度
        playStateForOrder:0 , // 0 顺序循环播放 1随机播放 2 单作品播放
        playMusicCurrentTime:0,
        playMusicCurrentTimeText:'0:00', //播放当前时间
        playMusicMaxDuringTime:0,
        playMusicMaxDuringTimeText:'--:--', //播放总时间
        randomPlayOrderArray:[], //记录随机数数组 判断是否重复使用
        isPause:false,
        isLikeConnect:false, //是否正在连接喜欢接口
        isHaveLikedCancelOrLike:false , // 是否有已经操作过喜欢或不喜欢
        isHaveLikedIndexArray:[], //记录操作过是否喜欢的数组
        //小窗口控制使用
        playControlBgTop:-150, //初始背景位置
        controlBgTimer:null, //移动计时
        controlSmallX:0, //上次移动位置 x
        controlSmallY:0, //上次移动位置 y
        controlSmallTag:0, //标记是否向上移动
        //大窗口控制使用
        isShowFullControl:false,
        isShowFullButton:false,
        fullConrolBgTop:0,
        fullConrolBgTopHeight:'',
        fullControlBGScroll:0,
        fullControlBGScollHeight:'',
        fullControlBGTopPadding:110,
        controlFullX:0, //上次移动位置 x
        controlFullY:0, //上次移动位置 y
        fullControlBGScrollTimer:null,
        controlFullTag:0, //标记是否向上移动
        playbarLeftButtonDistence:0,
        //loading动画使用
        showModalStatus:false, //是否显示 loading 旋转动画
        turnLoadingTimer:null, //loading 旋转动画
        loadingRoateIndex:0, //loading 旋转动画
        animationLoadingData:{}, //loading 旋转动画
        isShowNoDataText:false, //是否 展示'请收藏您喜欢的音乐'
        isOnSeeking:0, //是否正在跳转 0非 1是
        isOnSeekingArray:[], //跳转完成后 记录当前播放时间节点,如果连续5秒都是播放表示跳转失败从 5秒继续播放
        isOnSeekingType:null, //判断往前还是往后 pre,nxt
        seekingStartLength:0,
    },

    onLoad: function () {
        var context = this;
        // 创建动画
        this.animation = wx.createAnimation({
            duration: 500,
            timingFunction: "linear",
            success: function (res) {
                this.data.isMoveIng = false
            }
        })

    },
    onReachBottom: function (){
        return;
        // 翻页
        // if ( this.data.isPageNext ){
        //     this.data.pageno++;
        //     this.configDataFromNetwork();
        // } else{
        //     return;
        // }

    },
    onPullDownRefresh: function(){
        return;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            isShowNoDataText:false,
        })
        //重新打开时 重置数据 并 设置正在收听状态
        this.configDataFromNetwork();
        //修改封面图宽高
        this.getSystemInfo();
        //重新打开时 重置相关数值
        this.normalStateControl();
    },
    initData:function (){

    },
    //重新打开时 重置相关数值
    normalStateControl:function () {
        var that = this;

        // 判断是否在favor 并正在播放
        if (app.globalData.playPage!='favor'&&app.globalData.playPageProIndex==-1){
            return;
        }
        //// 重新打开时 判断是否暂停
        var isPause=backgroundAudioManager.paused ;
        if (typeof isPause =='undefined'){
            return;
        }
        this.data.isPause = isPause;
        this.setData({
            isPause:this.data.isPause,
        })
        //// 重新打开时 打开小窗口

        this.controlBackOpen();

        this.controlFullBackClose();
        // 重新打开时 设置之前播放状态
        this.data.playStateForOrder=app.globalData.favorPlayStateForOrder;
        this.setData({
            playStateForOrder:this.data.playStateForOrder
        })
    },
    //--------------------- 点击播放 ---------------------
    clickToPlay: function (e) {
        console.log(e.currentTarget.dataset.index);
        var index= e.currentTarget.dataset.index;
        this.setIsLikeIsHaveToResetData();
        //设置播放下一首 index 为作品坐标
        this.playMusicListFromProDataIndex(index);
        // 打开小背景
        this.controlBackOpen();
    },

    playMusicListFromProDataIndex:function(index){

        this.setFavorStateListUI(index);
        app.globalData.playPage = 'favor';
        app.globalData.playPageProIndex = index;
        app.globalData.playList = this.data.networkFormatData[index].mp3List;
        app.globalData.playMusicIndex = 0;
        //置空 播放时间 播放总时间
        this.setNormalDataForPlayTime();
        this.setData({
            playingIndex:app.globalData.playMusicIndex,
        })
        app.musicControllerForState(0);
        this.setIndexTimerState('run');
        this.timerController();
    },
    timerController: function(){
        var that = this;

        that.data.indexTimer = setInterval(function () {
            try {

                //切换下一曲
                that.setNextProMusicOnUpdate();

                if (app.globalData.appIsOnShow==false){
                    console.log('-----------favor.js  timer    后台---------')
                    // setTimeout(that.timerController(),0);
                    return;
                }else{
                    //判断是否在favor页
                    that.judgeIsOnOtherPage();
                    //总时间
                    var duration = Math.ceil(backgroundAudioManager.duration )
                    console.log('f总时长'+duration)
                    //当前时间
                    var currentPosition = Math.ceil(app.globalData.playMusicCurrentLength )
                    console.log( 'f播放进度 '+currentPosition)
                    if (that.seekJudgeIsError()){
                        //时间更新 更新进度条
                        that.setProcessBarOnUpdate();
                        //时间更新 更新页面当前播放时间
                        that.setChangeMusicDuringText();
                    }
                    //时间更新 根据index切换播放章节
                    that.setChangeSongOnUpdate();

                    //时间更新 更新总时间
                    that.setMaxLengthOnUpdate();

                    // if (stopTimerControl==false){
                    //     setTimeout(that.timerController(),0);
                    // }else{
                    //     console.log('-----------关闭favor.js  timer---------')
                    // }
                }

            } catch (e) { }
        }, 500)
    },
    //-----------------------时间更新控制-----------------
    //时间更新 更新进度条
    setProcessBarOnUpdate:function(){
        var that = this;
        //当前时间
        var currentPosition = Math.ceil(app.globalData.playMusicCurrentLength )
        //总时间
        var duration = Math.ceil(backgroundAudioManager.duration )
        if (typeof  currentPosition != 'undefined'&&currentPosition != 0&&duration!=0&&typeof duration!='undefined'&&!isNaN(duration)){
            //根据播放进度 设置播放按钮动画
            var singL=this.data.playBarControlWidthMax/(duration);
            that.data.playbarLeftButtonDistence=singL*currentPosition;
            that.setData({
                playbarLeftButtonDistence:that.data.playbarLeftButtonDistence
            })
        }

    },
    seekJudgeIsError:function(){
        var currentPosition = Math.ceil(app.globalData.playMusicCurrentLength )
        if (this.data.isOnSeeking==1||currentPosition==1){
            this.data.isOnSeekingArray.push(currentPosition);
            console.log('正在跳转计时 当前播放时间',currentPosition);
            if (this.data.isOnSeekingArray.length%6==0||currentPosition==this.data.playingMusicMaxRecord){
                console.log('跳转出错 下一秒从正在播放跳转开始播放');
                this.data.isOnSeeking=0;
                this.data.isOnSeekingArray=[];
                this.data.isOnSeekingType=null;
            }
            return false;
        }
        this.data.isOnSeekingArray=[];
        this.data.isOnSeeking=0;
        this.data.isOnSeekingType=null;
        return true;
    },
    //时间更新 更新页面当前播放时间
    setChangeMusicDuringText:function(){
        var that = this;
        //当前时间
        var currentPosition = Math.ceil(app.globalData.playMusicCurrentLength )
        var appCurrentPosition  = Math.ceil(backgroundAudioManager.currentTime )
        if (typeof  currentPosition != 'undefined' && currentPosition != 0 &&!isNaN(currentPosition)){
            that.data.playMusicCurrentTime=currentPosition;
            var newCurrentTimetext = util.secToTimefunction(appCurrentPosition);
            if (that.data.playMusicCurrentTimeText!=newCurrentTimetext) {
                that.setData({
                    playMusicCurrentTimeText:newCurrentTimetext
                })
            }

        }
    },
    //时间更新 需要切换下一首
    setNextProMusicOnUpdate:function(){

        if (app.globalData.playPage!='favor'){
            return
        }
        if (app.globalData.isNeedChangePro  == 1||app.globalData.isNeedChangeFavorNext  == 1){
            console.log('需要切换下一首');
            app.globalData.isNeedChangePro = 0;
            app.globalData.isNeedChangeFavorNext  = 0;
            //如果有取消喜欢或者继续喜欢 需要重置数据
            this.setIsLikeIsHaveToResetData();
            console.log('f播放作品结束');
            this.musicPlayNextMenu(true);
        }

        if (app.globalData.isNeedChangeFavorPre  == 1){
            console.log('需要切换上一首');
            app.globalData.isNeedChangeFavorPre  = 0;
            //如果有取消喜欢或者继续喜欢 需要重置数据
            this.setIsLikeIsHaveToResetData();
            console.log('f播放作品结束');
            this.musicPlayPreMenu();
        }


    },
    //时间更新 根据index切换播放章节
    setChangeSongOnUpdate:function(){
        var that = this;
        //如果切换歌曲 切换选中状态
        if (app.globalData.playMusicIndex != that.data.playingIndex){
            that.data.playingIndex = app.globalData.playMusicIndex;
            that.setData({
                playingIndex:that.data.playingIndex
            })
        }
        if (this.data.hasListened != app.globalData.hasListened) {
            that.setData({
                hasListened:app.globalData.hasListened
            })
        }

    },
    //时间更新 更新总时间
    setMaxLengthOnUpdate: function(){
        var that = this;
        //总时间
        var duration = Math.ceil(backgroundAudioManager.duration )

        if (typeof  duration != 'undefined' && duration != 0 &&!isNaN(duration)&&duration!=0&&typeof duration!='undefined'){

            var newDuringTimeText = util.secToTimefunction(duration);
            if (that.data.playMusicMaxDuringTimeText!=newDuringTimeText){
                that.data.playMusicMaxDuringTime=duration;
                that.setData({
                    playMusicMaxDuringTimeText:newDuringTimeText
                })
            }

        }
    },

    //判断是否在favor页
    judgeIsOnOtherPage:function(){
        if (app.globalData.playPage !='favor'){
            // this.data.isLimitByChange=1;
            // this.data.isPause=false;
            this.setIndexTimerState('stop')
            this.setData({
                // isPause:this.data.isPause,
                // playbarLength:0,
            })
        }else{
            var isPauseManager = backgroundAudioManager.paused;
            if (typeof isPauseManager == 'undefined'){
                console.log('未开始播放 error')
                return;
            }
            this.data.isPause = isPauseManager;
            this.setData({
                isPause:this.data.isPause
            })
        }

    },

    configDataFromNetwork:function () {

        var that = this
        if (app.globalData.favorNetworkData.length>0){
            this.data.networkFormatData=app.globalData.favorNetworkData;
            this.setData({
                list: that.data.networkFormatData
            })
            // 设置正在收听状态
            this.setLiseningState();
        } else{
            this.showModal();
        }
        requestApi.kukeGetFavoriteList(this.data.pageno,10000).then(res=>{
            console.log('configDataFromNetwork-----------------')
            // console.log(res)
            that.setNetworkFormatData(res);
            that.hideModal();
            // 设置数据
            that.setData({
                list: that.data.networkFormatData
            })
            // 设置正在收听状态
            that.setLiseningState();
        })

    },


    //--------------------设置收听状态----------------
    // 设置正在收听状态
    setLiseningState:function(){
        if (app.globalData.playPageProIndex!=-1){
            var indexPro=app.globalData.playPageProIndex;
            this.data.playingIndex=app.globalData.playMusicIndex;
            this.setData({
                playingIndex:app.globalData.playMusicIndex,
            })
            this.setFavorStateListUI(indexPro);
        }
    },
    // 设置正在收听 UI
    setFavorStateListUI:function(index){
        for (var i=0;i < this.data.networkFormatData.length;i++){

            if (i==index){
                if (this.data.networkFormatData[i].isPlaying == 1){
                    return;
                }
                this.data.networkFormatData[i].isPlaying = 1;
            } else{
                this.data.networkFormatData[i].isPlaying = 0;
            }
        }
        this.data.playingCurrentPro=index;
        this.setData({
            list: this.data.networkFormatData,
            playProUIIndex:index
        });
    },
    //--------------------- 进度条控制 ---------------------
    touchProcessStart: function (e) {

        if (e.touches.length == 1) {
            this.setProcessPauseState('pause');

            this.data.seekingStartLength=this.data.playbarLength;
        }
    },
    touchProcessMove: function (e) {

        var that = this
        if (e.touches.length == 1) {
            //手指移动时水平方向位置
            var moveX = e.touches[0].clientX;
            // console.log(e);
            //手指起始点位置与移动期间的差值
            var disX = moveX;
            //
            //     var txtStyle = "";
            if (disX == 0 || disX < 0){
                this.data.playbarLeftButtonDistence=1;
            } else if (disX > this.data.playBarControlWidthMax) {
                this.data.playbarLeftButtonDistence=this.data.playBarControlWidthMax;
            }else{
                this.data.playbarLeftButtonDistence=disX*1.68;
            }
            if (this.data.playbarLeftButtonDistence > this.data.playBarControlWidthMax) {
                this.data.playbarLeftButtonDistence = this.data.playBarControlWidthMax;
            }
            console.log(this.data.playbarLeftButtonDistence);
            // playMusicCurrentTime
            var singL = this.data.playBarControlWidthMax / (this.data.playMusicMaxDuringTime);
            // console.log('singL'+this.data.playingMusicMaxLength)
            // playMusicCurrentTime
            this.data.playMusicCurrentTimeText=util.secToTimefunction(Math.ceil(this.data.playbarLeftButtonDistence /singL));
            that.setData({
                playbarLeftButtonDistence: this.data.playbarLeftButtonDistence,
                playMusicCurrentTimeText: this.data.playMusicCurrentTimeText,
            });
            // that.setData({
            //     playbarLeftButtonDistence: this.data.playbarLeftButtonDistence
            // });



        }
    },
    touchProcessEnd: function (e) {
        if (this.data.isLimitByChange==1){
            return;
        }
        var that = this;
        if (e.changedTouches.length == 1) {

            that.setProcessPauseState('play');
            //根据播放进度 设置播放按钮动画
            var singL =  that.data.playBarControlWidthMax / (app.globalData.playMusicLength);
            app.globalData.playMusicCurrentLength = that.data.playbarLeftButtonDistence /singL;
            // backgroundAudioManager.seek(Math.ceil(app.globalData.playMusicLength))
            if ( app.globalData.playMusicCurrentLength>=app.globalData.playMusicLength){
                this.data.isOnSeeking=0;
                this.data.isOnSeekingType=null;
            }else if (this.data.seekingStartLength>=this.data.playbarLength) {
                this.data.isOnSeeking=1;
                this.data.isOnSeekingType='pre';
            }else if(this.data.seekingStartLength<this.data.playbarLength){
                this.data.isOnSeeking=1;
                this.data.isOnSeekingType='next';
            }
            that.data.playMusicCurrentTime=app.globalData.playMusicCurrentLength;
            app.musicControllerForPause(Math.ceil(app.globalData.playMusicCurrentLength));



        }
    },
    clickProcessMain: function (e){
        var that = this;
        console.log(e.changedTouches[0].clientX);
        var distX = e.changedTouches[0].clientX*1.68;
        if (distX > this.data.playBarControlWidthMax){
            distX = this.data.playBarControlWidthMax
        } else if(distX<0){
            distX = 0;
        }
        that.data.playbarLeftButtonDistence = distX;
        that.setProcessPauseState();
        var duration = Math.ceil(backgroundAudioManager.duration );
        var singL = this.data.playBarControlWidthMax / (duration);
        app.globalData.playMusicLength = that.data.playbarLeftButtonDistence /singL;
        backgroundAudioManager.seek(Math.ceil(app.globalData.playMusicLength));
    },
    setProcessPauseState: function(stateType){
        var isPauseManager = backgroundAudioManager.paused;
        if (typeof isPauseManager == 'undefined'){
            console.log('未开始播放 error')
            return;
        }
        //判断是否需要 暂停 pause 或者 播放 play
        if (stateType=='play'){
            this.data.isPause = false;
            this.setIndexTimerState('run');
            this.timerController();
        } else{
            this.data.isPause = true;
            this.setIndexTimerState('stop');
            backgroundAudioManager.pause();
        }
        // this.data.isPause = isPauseManager;
        // if (this.data.isPause == true){
        //     this.data.isPause = false;
        //     backgroundAudioManager.play();
        // }
        this.setData({
            isPause:this.data.isPause
        })
    },

    //--------------------- 跳转 ---------------------
    clickToNavigation:function (e) {
        if (e.currentTarget.dataset.type == 'cardDetail'){
            wx.navigateTo({
                //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
                url:"/pages/cardDetail/cardDetail?cardId="+e.currentTarget.dataset.id
            })
        }
        if (e.currentTarget.dataset.type == 'back'){
            wx.navigateBack({
                //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
                delta:1
            })
        }
    },


    //--------------------- 删除按钮控制 ---------------------
    touchS: function (e) {
        if (e.touches.length == 1) {
            this.setData({
                //设置触摸起始点水平方向位置
                startX: e.touches[0].clientX
            });
        }
    },


    touchM: function (e) {
        var that = this

        if (e.touches.length == 1) {
            //手指移动时水平方向位置
            var moveX = e.touches[0].clientX;
            // console.log(e);
            //手指起始点位置与移动期间的差值
            var disX = this.data.startX - moveX;
            console.log('删除移动差距'+disX);
            var delBtnWidth = this.data.delBtnWidth;
            var txtStyle = "";
            if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
                txtStyle = "left:0px";
            } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
                // txtStyle = "left:-" + delBtnWidth + "px";
                if (disX >= delBtnWidth*2) {
                    //控制手指移动距离最大值为删除按钮的宽度
                    txtStyle = "left:-" + delBtnWidth + "px";
                }
            }
            //获取手指触摸的是哪一项
            var index = e.currentTarget.dataset.index;
            this.data.networkFormatData[index].txtStyle = txtStyle;
            //更新列表的状态
            this.setData({
                list: this.data.networkFormatData
            });
        }
    },

    touchE: function (e) {
        if (e.changedTouches.length == 1) {
            console.log(e);
            //手指移动结束后水平位置
            var endX = e.changedTouches[0].clientX;
            //触摸开始与结束，手指移动的距离
            var disX = this.data.startX - endX;
            var delBtnWidth = this.data.delBtnWidth;
            //如果距离小于删除按钮的1/2，不显示删除按钮
            var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
            //获取手指触摸的是哪一项
            var index = e.currentTarget.dataset.index;

            this.data.networkFormatData[index].txtStyle = txtStyle;
            //更新列表的状态
            this.setData({
                list: this.data.networkFormatData
            });
            for (var i =0 ;i<this.data.networkFormatData.length;i++){
                if (i!=index){
                    this.data.networkFormatData[index].txtStyle='';
                }
            }
        }
    },
    //获取元素自适应后的实际宽度
    getEleWidth: function (w) {
        var real = 0;
        try {
            var res = wx.getSystemInfoSync().windowWidth;
            var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
            // console.log(scale);
            real = Math.floor(res / scale);
            return real;
        } catch (e) {
            return false;
            // Do something when catch error
        }
    },

    initEleWidth: function () {
        var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
        this.setData({
            delBtnWidth: delBtnWidth
        });
    },
    //点击删除按钮事件
    delItem: function (e) {
        var that = this
        wx.showModal({
            title: '提示',
            content: '是否删除？',
            success: function (res) {
                if (res.confirm) {
                    //获取列表中要删除项的下标
                    var index = e.currentTarget.dataset.index;
                    requestApi.kukeSetFavoriteCollect(
                        that.data.networkFormatData[index].itemcode,
                        that.data.networkFormatData[index].workId,
                        0
                    ).then(res=>{
                        //移除列表中下标为index的项
                        that.data.networkFormatData.splice(index, 1);
                        //更新列表的状态
                        that.setData({
                            list: that.data.networkFormatData
                        });
                    })
                } else {
                    initdata(that)
                }
            }
        })

    },



    //--------------------小窗口控制------------------
    // 打开小背景
    controlBackOpen:function () {
        var that = this;
        this.data.controlBgTimer = setInterval(function () {
            if(that.data.playControlBgTop>=-20){
                clearInterval(that.data.controlBgTimer);
                that.data.playControlBgTop=0;
                that.setData({
                    playControlBgTop: that.data.playControlBgTop
                })
                return
            }
            that.data.playControlBgTop+=30;
            that.setData({
                playControlBgTop: that.data.playControlBgTop
            })
        },10)
    },
    // 打开关闭小背景
    controlBackClose:function () {

    },
    //滑动可控制
    touchSmallBGStart:function(event){
        console.log('touchSmallBGStart')
        this.data.controlSmallTag=1;
    },
    //向上移动
    touchSmallBGMove:function (event) {
        console.log(event)
        let currentX = event.touches[0].pageX
        let currentY = event.touches[0].pageY
        let tx = currentX - this.data.controlSmallX
        let ty = currentY - this.data.controlSmallY
        if (ty < 0){
            this.data.controlSmallTag=1;

        }
        this.data.controlSmallX = currentX
        this.data.controlSmallY = currentY
    },
    touchSmallBGEnd:function(event){
        console.log('touchSmallBGEnd')
        if (this.data.controlSmallTag==1) {
            this.data.controlSmallTag=0;
            console.log('"向上滑动"');

            this.controlFullBackOpen();

        }
    },
    //--------------------大窗口控制------------------
    // 打开大背景
    controlFullBackOpen:function () {
        if (isChangeSmall==1){
            isChangeSmall=0;
            return
        }
        var that = this;
        that.setData({
            isShowFullControl: true,
        })
        this.data.fullControlBGScrollTimer = setInterval(function () {
            if(that.data.fullControlBGScroll<=that.data.fullControlBGTopPadding){
                clearInterval(that.data.fullControlBGScrollTimer);
                that.data.fullControlBGScroll = that.data.fullControlBGTopPadding;

                that.setData({
                    fullControlBGScroll: that.data.fullControlBGScroll,
                    fullControlBGScrollHeight:that.data.fullControlBGScroll,
                    isShowFullButton:true,
                    isShowFullControl: true,
                })
                return
            }
            that.data.fullControlBGScroll-=20;
            that.setData({
                fullControlBGScroll: that.data.fullControlBGScroll,
                fullControlBGScrollHeight:that.data.fullControlBGScroll,
            })
        },5)
    },
    // 关闭大背景
    controlFullBackClose:function () {
        var that = this;
        this.setData({
            isShowFullButton:false,
        })
        this.data.fullControlBGScrollTimer = setInterval(function () {
            if(that.data.fullControlBGScroll>=that.data.screenHeight){
                clearInterval(that.data.fullControlBGScrollTimer);
                that.data.fullControlBGScroll = that.data.screenHeight;
                that.setData({
                    fullControlBGScroll: that.data.fullControlBGScroll,
                    fullControlBGScrollHeight:that.data.fullControlBGScroll,
                    isShowFullControl:false,
                    isShowFullButton:false,
                })
                return
            }
            that.data.fullControlBGScroll+=20;
            that.setData({
                fullControlBGScroll: that.data.fullControlBGScroll,
                fullControlBGScrollHeight:that.data.fullControlBGScroll,
            })
        },5)
    },
    //滑动可控制
    touchFullBGStart:function(event){
        // console.log('touchFullBGStart')
        // this.data.controlFullTag=1;
    },
    //向上移动
    touchFullBGMove:function (event) {
        console.log(event)
        let currentX = event.touches[0].pageX
        let currentY = event.touches[0].pageY
        let tx = currentX - this.data.controlFullX
        let ty = currentY - this.data.controlFullY
        if (ty > 0){
            this.data.controlFullTag=1;
        }else{
            this.data.controlFullTag=0;
        }
        this.data.controlFullX = currentX
        this.data.controlFullY = currentY
    },
    touchFullBGEnd:function(event){
        // console.log('touchFullBGEnd')
        if (this.data.controlFullTag==1) {
            this.data.controlFullTag=0;
            console.log('"向下滑动"');
            this.controlFullBackClose();

        }
    },
    clickCloseFullBG:function(){
        console.log('"向下滑动"');
        this.controlFullBackClose();
    },
    //   ------------------控制台 播放控制--------------------
    //点击更改播放顺序 0 顺序循环播放 1随机播放 2 单作品循环播放
    clickMusicToOrderButton:function(e){
        isChangeSmall=1;
        if (this.data.playStateForOrder==2){
            this.data.playStateForOrder=0;
        } else{
            this.data.playStateForOrder++;
        }
        // 重新打开时 设置之前播放状态
        app.globalData.favorPlayStateForOrder=this.data.playStateForOrder;
        this.setData({
            playStateForOrder:this.data.playStateForOrder
        })
    },
    //点击播放上一个作品 如果为第一个 则播放第一个作品
    clickMusicToPreButton:function(){
        this.setIsLikeIsHaveToResetData();
        this.musicPlayPreMenu();
        // console.log()
    },
    //点击播放下一个作品 如果为最后一个 则播放第一个作品
    clickMusicToNextButton:function(){
        //如果有取消喜欢或者继续喜欢 需要重置数据
        this.setIsLikeIsHaveToResetData();
        this.musicPlayNextMenu(false);
    },
    //点击暂停 或者播放
    clickMusicToPauseOrPlayButton:function(){
        if (this.data.isOnSeeking==1){
            return;
        }
        // 已经暂停
        if (this.data.isPause==true){
            this.data.isPause=false;
        } else{
            // 正在播放
            this.data.isPause=true;
        }
        //关闭或者播放
        this.musicPauseOrPlayMenu(this.data.isPause);
    },
    //点击取消喜欢 或 继续喜欢
    clickMusicToLikeButton:function(e){
        var playProIndex = e.currentTarget.dataset.index;
        var isLike = this.data.networkFormatData[playProIndex].isLike ;
        if (this.data.isLikeConnect==0){
            this.data.isLikeConnect=1;
            var that = this
            if(this.data.networkFormatData[playProIndex].isLike == 0){
                this.data.networkFormatData[playProIndex].isLike = 1
            }else{
                this.data.networkFormatData[playProIndex].isLike = 0;
            }

            requestApi.kukeSetFavoriteCollect(
                this.data.networkFormatData[playProIndex].itemcode,
                this.data.networkFormatData[playProIndex].workId,
                this.data.networkFormatData[playProIndex].isLike
            ).then(res=>{
                that.data.isLikeConnect=0;
                that.data.isHaveLikedCancelOrLike=true;
                if (that.data.networkFormatData[playProIndex].isLike == 0){
                    that.data.isHaveLikedIndexArray.push(playProIndex);
                }else{
                    for (let i = 0; i < that.data.isHaveLikedIndexArray.length; i++) {
                        if ( that.data.isHaveLikedIndexArray[i]==playProIndex) {
                            that.data.isHaveLikedIndexArray.splice(i,1);
                        }
                    }
                }

                console.log('请求是否喜欢成功'+ that.data.networkFormatData[playProIndex].isLike);
                that.setData({
                    list:that.data.networkFormatData,
                })

                // 重置数据
                // that.configDataFromNetwork();
            })

        }
    },
    //点击歌单列表
    clickMusicList: function (e){
        if (e.currentTarget.dataset.index != this.data.playingIndex) {
            console.log(e.currentTarget.dataset.index);
            this.data.playingIndex = e.currentTarget.dataset.index;
            app.globalData.playMusicIndex = e.currentTarget.dataset.index;
            this.data.playbarLeftButtonDistence = 0;
            this.setData({
                playingIndex:this.data.playingIndex,
                playbarLeftButtonDistence: this.data.playbarLeftButtonDistence,
            })
            this.setNormalDataForPlayTime();
            app.musicControllerForState(0);
        }

    },
    //-------------------- 音频控制 ---------------------
    //  播放下一作品
    musicPlayNextMenu:function(isUpdateForTime){

        app.globalData.isNeedChangePro = 0;
        if (this.data.networkFormatData.length==0){
            console.log('播放列表 无内容可播放')
            return;
        }
        var playProNext = this.data.playingCurrentPro;
        // 0 顺序循环播放 1随机播放 2 单作品循环播放
        switch (this.data.playStateForOrder){
            case 0:
                if (this.data.networkFormatData.length-1<=this.data.playingCurrentPro) {
                    console.log('播放列表已播放完成 循环 从头开始')
                    playProNext=0;
                }else{
                    console.log('顺序播放')
                    playProNext++;
                }
                break;
            case 1:
                //生成随机听 maxRepeatCount为几个作品内不重复 并返回新生成坐标
                playProNext=this.randomNumberSetting(5);
                console.log('随机播放 '+playProNext)
                break;
            case 2:
                if (isUpdateForTime){
                    console.log('单曲循环 重复收听')
                } else{
                    console.log('点击下一首 单曲循环 ')
                    if (this.data.networkFormatData.length-1<=this.data.playingCurrentPro) {
                        console.log('单曲循环 播放列表已播放完成 循环从头开始')
                        playProNext=0;
                    }else{
                        console.log('单曲循环 下一首')
                        playProNext++;
                    }
                }

                break;
        }
        this.data.playingCurrentPro = playProNext;
        this.playMusicListFromProDataIndex(this.data.playingCurrentPro);
    },
    // 顺序 播放上一作品
    musicPlayPreMenu:function(){

        app.globalData.isNeedChangePro = 0;
        var playProNext = this.data.playingCurrentPro;
        // 0 顺序循环播放 1随机播放 2 单作品循环播放
        switch (this.data.playStateForOrder){
            case 0:
                if (this.data.playingCurrentPro==0) {
                    console.log('顺序播放上一个 已经第一个 继续播放第一个')
                    playProNext=0;
                }else{
                    console.log('顺序播放上一个')
                    playProNext--;
                }
                break;
            case 1:
                //生成随机听 maxRepeatCount为几个作品内不重复 并返回新生成坐标
                playProNext=this.randomNumberSetting(5);
                console.log('随机播放 '+playProNext)
                break;
            case 2:
                if (this.data.playingCurrentPro==0) {
                    console.log('单曲循环 播放上一个 已经第一个')
                    playProNext=0;
                }else{
                    console.log('单曲循环 上一个')
                    playProNext--;
                }
                break;
        }
        this.data.playingCurrentPro = playProNext;
        this.playMusicListFromProDataIndex(this.data.playingCurrentPro);

    },
    //暂停或者播放
    musicPauseOrPlayMenu:function(isPause){
        var isPauseManager=backgroundAudioManager.paused ;
        // 未播放 出错
        if (!util.isNotEmptyString(isPauseManager)){
            console.log('未开始播放 error')
            // return;
        }
        //暂停曲子
        if (isPause == true){
            backgroundAudioManager.pause();
        } else {
        //    继续播放
            backgroundAudioManager.play();
        }
        this.setData({
            isPause:this.data.isPause
        })

    },
    //   ---------------------页面控制---------------------
    getSystemInfo:function () {
        var that = this;
        var wxGetSystemInfo=requestApi.wxGetSystemInfo()
        wxGetSystemInfo().then(res=>{
            var windowWidth = res.windowWidth;
            var windowHeight = res.windowHeight;
            if (util.systemInfoToPhoneHeightIsX(res)=='X') {
                that.setData({
                    isIphoneX: true,
                })
            } else if(util.systemInfoToPhoneHeightIsX(res)=='N'){
                that.setData({
                    isIphoneX: false,
                })
            }
            // context.data.fullConrolBgTop=windowHeight-80;
            that.setData({
                screenWidth: windowWidth,
                screenHeight:windowHeight,

            })
            that.data.fullControlBGScroll= windowHeight;
            that.data.fullControlBGTopPadding=(windowHeight*0.17);
            that.setData({
                fullConrolBgTop:windowHeight,
                fullConrolBgTopHeight:windowHeight,
                fullControlBGScroll:that.data.fullControlBGScroll,
                fullControlBGScrollHeight:that.data.fullControlBGScroll,
            })
        })
    },
    //    ---------------------数据设置---------------------
    //格式化网络数据
    setNetworkFormatData:function (res) {
        this.data.networkFormatData=[];
        var that = this;
        if (res.data.data.length==0){
            this.setData({
                isShowNoDataText:true
            })
            return;
        } else {
            this.setData({
                isShowNoDataText:false
            })
        }

        // var favorData ={};
        for (var i = 0 ;i<res.data.data.length;i++){
            var workInfoData = res.data.data[i].workInfo;

            if (!util.isNotEmptyString(workInfoData)){
                console.log('error------------本条收藏数据无信息')
                console.log(workInfoData)
                return;
            }

            if (res.data.data.length<20){
                that.data.isPageNext = false;
            }
            var favorData= util.formatNetworkProMusicData(res.data.data[i]);
            that.data.networkFormatData.push(favorData);
        }
        app.globalData.favorNetworkData=that.data.networkFormatData;

    },
    //置空 播放时间 播放总时间
    setNormalDataForPlayTime:function(){
        this.data.playMusicMaxDuringTimeText='--:--';
        this.data.playMusicCurrentTimeText='0:00';
        this.data.isOnSeeking=0;
        this.data.isOnSeekingType=null;
        this.setData({
            playMusicMaxDuringTimeText:this.data.playMusicMaxDuringTimeText,
            playMusicCurrentTimeText:this.data.playMusicCurrentTimeText,
        })
    },
    // 判断是否取消过收藏 ，如果取消过 ，则当触发 切换作品时候 重置列表
    setIsLikeIsHaveToResetData:function(){
        if (this.data.isHaveLikedCancelOrLike){
            console.log('已取消或重新喜欢 需重置数据')
            this.data.isHaveLikedCancelOrLike=false;
            for (let i = 0; i < this.data.isHaveLikedIndexArray.length; i++) {
                this.data.networkFormatData.splice(this.data.isHaveLikedIndexArray[i],1);
            }
            app.globalData.favorNetworkData = this.data.networkFormatData;
            this.data.isHaveLikedIndexArray=[];
            this.setData({
                list:this.data.networkFormatData,
            })
        }
    },
    stopScorll:function () {
        return;
    },
    //生成随机听 maxRepeatCount为几个作品内不重复
    randomNumberSetting:function (maxRepeatCount){
        console.log('开始生成随机数-----------------')
        var maxLength=this.data.networkFormatData.length;
        if (maxLength<=maxRepeatCount) {
            console.log('收藏数小于 '+maxRepeatCount+'个，随机不计算重复')
            var randomLess=Math.floor(Math.random()*(maxLength-1+1));
            return randomLess;
        }
        var isNotSame = true;
        while (isNotSame)  {
            var isNotSameCount=0;
            var random=Math.floor(Math.random()*(maxLength-1+1));
            console.log('本次随机生成'+random);
            console.log('已存储数组')
            console.log(this.data.randomPlayOrderArray)
            for (var i=0;i<this.data.randomPlayOrderArray.length;i++){
                if (this.data.randomPlayOrderArray[i]==random) {
                    isNotSameCount++
                    console.log('5次出现重复')
                }
            }
            if (isNotSameCount == 0){
                isNotSame = false
                this.data.randomPlayOrderArray.push(random);
                console.log('----------------未出现重复加入')
            }
        }
        if (this.data.randomPlayOrderArray.length>maxRepeatCount){
            this.data.randomPlayOrderArray.splice(0, 1);
        }
        return this.data.randomPlayOrderArray[this.data.randomPlayOrderArray.length-1];
    },
    //--------------------- 分享控制 ---------------------


    onShareAppMessage: function( options ){
        var that = this;
        console.log(that.data.networkFormatData[that.data.playingCurrentPro]);
        // 设置菜单中的转发按钮触发转发事件时的转发内容
        var shareObj = {
            title: that.data.networkFormatData[that.data.playingCurrentPro].mp3Title,        // 默认是小程序的名称(可以写slogan等)
            path:  '/pages/index/index?workId='+that.data.networkFormatData[that.data.playingCurrentPro].workId,        // 默认是当前页面，必须是以‘/’开头的完整路径
            imageUrl: that.data.networkFormatData[that.data.playingCurrentPro].mp3Img,     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
            success: function(res){
                // 转发成功之后的回调
                if(res.errMsg == 'shareAppMessage:ok'){
                    console.log('转发成功之后的回调');
                    console.log(res)
                    that.controlFullBackClose();
                }
            },
            fail: function(res){
                // 转发失败之后的回调
                if(res.errMsg == 'shareAppMessage:fail cancel'){
                    console.log('用户取消转发');
                    console.log(res)
                    // 用户取消转发
                }else if(res.errMsg == 'shareAppMessage:fail'){
                    // 转发失败，其中 detail message 为详细失败信息
                    console.log('转发失败');
                    console.log(res)
                }
                that.controlFullBackClose();
            },
        }
        // 来自页面内的按钮的转发
        if( options.from == 'button' ){
            var eData = options.target.dataset;
            console.log('来自页面内的按钮的转发');
            console.log( eData.name );     // shareBtn
            // 此处可以修改 shareObj 中的内容
            shareObj.path = '/pages/index/index?workId='+that.data.networkFormatData[that.data.playingCurrentPro].workId

        }
        // 返回shareObj
        return shareObj;
    },
    //    ---------------------测试---------------------
    clickTestSeek2:function () {

        // return;
        //总时间
        var duration = Math.ceil(backgroundAudioManager.duration )
        //当前时间
        var currentPosition = Math.ceil(backgroundAudioManager.currentTime )
        backgroundAudioManager.seek(Math.ceil(duration-2))
    },

    clickRetrun:function () {
        return
    },
    //--------------------- 遮罩控制 ---------------------
    //关闭或者开启 indexTimer state:run  || stop
    setIndexTimerState:function(state){
        clearInterval(this.data.indexTimer);
        if(state=='run'){

            stopTimerControl=false;
            this.data.indexTimer=null;
        }else if (state=='stop') {

            stopTimerControl=true;
            this.data.indexTimer=null;
        }
    },
    showModal: function () {
        var that = this
        // 显示遮罩层
        this.data.turnLoadingTimer = setInterval(function () {
            that.data.loadingRoateIndex++;
            that.animation.rotate(5 * (that.data.loadingRoateIndex)).step();
            that.setData({
                animationLoadingData: that.animation.export()
            })
        }, 20)
        this.setData({
            showModalStatus: true
        })

    },
    hideModal: function () {
        clearInterval(this.data.turnLoadingTimer);
        this.setData({
            showModalStatus: false
        })

    },
})
